package com.accenture.fd.controller;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.accenture.fd.entity.SrcType;
import com.accenture.fd.service.CreateSourceService;
import com.accenture.fd.service.GetSrcTypeService;
import com.accenture.fd.service.OutputDesignService;

import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;

@CrossOrigin
@RestController
public class CreateSourceController {

	@Autowired
	private OutputDesignService outputDesignService;

	@Autowired
	private GetSrcTypeService srcTypeService;

	@Autowired
	private CreateSourceService createSourceService;

	@GetMapping(value = "/createSource")
	public void createSource(@RequestParam("screenId") String screenId,
			@RequestParam("classId") String classId,
			@RequestParam("classPakage") String classPakage,
			@RequestParam("classType") String classType, HttpServletResponse response) {

		//List<DesignScreenDto> designScreenDtoList = outputDesignService.getDesignScreenDtoListTemporary(screenId);
		// 生成模板填充数据
		Map<String, Object> tempData = this.createSourceService.createSource(classId, classPakage, classType);

		// 取得模板生成代码
		TemplateConfig config = new TemplateConfig();
		config.setCharset(StandardCharsets.UTF_8);
		config.setResourceMode(ResourceMode.CLASSPATH);
		config.setPath("/ftls");
		TemplateEngine templateEngine = TemplateUtil.createEngine(config);
		Template template = templateEngine.getTemplate(tempData.get("tempName").toString().concat(".ftl"));
		try {
			// 设置文件头：最后一个参数是设置下载文件名
			response.addHeader("Content-disposition",
					"attachment;filename=" + classId.concat(".java"));
			response.setHeader("Access-control-Expose-headers", "Content-disposition");
			ServletOutputStream out = response.getOutputStream();
			template.render(tempData, out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@GetMapping(value = "/createSourceJunit")
	public void createSourceJ(@RequestParam("screenId") String screenId,
			@RequestParam("classId") String classId,
			@RequestParam("classPakage") String classPakage,
			@RequestParam("classType") String classType, HttpServletResponse response) {

		//List<DesignScreenDto> designScreenDtoList = outputDesignService.getDesignScreenDtoListTemporary(screenId);
		// 生成模板填充数据
		Map<String, Object> tempData = this.createSourceService.createSource(classId, classPakage, classType);
		tempData = this.createSourceService.createJunit(tempData);

		//修改Map中的包名、Class名、tempName、追加属性Value

		// 取得模板生成代码
		TemplateConfig config = new TemplateConfig();
		config.setCharset(StandardCharsets.UTF_8);
		config.setResourceMode(ResourceMode.CLASSPATH);
		config.setPath("/ftls");
		TemplateEngine templateEngine = TemplateUtil.createEngine(config);
		Template template = templateEngine.getTemplate(tempData.get("tempName").toString().concat("Test.ftl"));
		try {
			// 设置文件头：最后一个参数是设置下载文件名
			response.addHeader("Content-disposition",
					"attachment;filename=" + classId.concat("Test.java"));
			response.setHeader("Access-control-Expose-headers", "Content-disposition");
			ServletOutputStream out = response.getOutputStream();
			template.render(tempData, out);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/getSrcTypes")
	public List<SrcType> getSrcTypes(HttpServletRequest request) {

		String keyword = request.getParameterMap().get("keyword")[0];

		return srcTypeService.getSrcType(keyword);

	}
}
